package com.gly091020.config;

import com.gly091020.BilibiliMedia;
import com.gly091020.util.BilibiliMediaUtil;
import com.gly091020.util.SimpleFileServer;
import com.terraformersmc.modmenu.api.ConfigScreenFactory;
import com.terraformersmc.modmenu.api.ModMenuApi;
import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.clothconfig2.api.ConfigBuilder;
import me.shedaniel.clothconfig2.api.ConfigCategory;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;

public class BilibiliMediaModMenuIntegration implements ModMenuApi {
    @Override
    public ConfigScreenFactory<?> getModConfigScreenFactory() {
        return parent -> {
            ConfigBuilder builder = ConfigBuilder.create()
                    .setParentScreen(parent)
                    .setTitle(Text.translatable("title.bilibili_media.config"))
                    .setDefaultBackgroundTexture(new Identifier(BilibiliMedia.ModID,  BilibiliMediaUtil.isGLY() ? "textures/gui/gly091020.png" : "icon.png"));
            ConfigCategory general = builder.getOrCreateCategory(Text.translatable("title.bilibili_media.config"));
            ConfigEntryBuilder entryBuilder = builder.entryBuilder();
            general.addEntry(entryBuilder.startBooleanToggle(Text.translatable("config.bilibili_media.enable"), BilibiliMedia.config.enable)
                    .setSaveConsumer(x->BilibiliMedia.config.enable = x)
                    .setDefaultValue(true)
                    .build());
            general.addEntry(entryBuilder.startBooleanToggle(Text.translatable("config.bilibili_media.use_bb_down"), BilibiliMedia.config.useBBDown)
                    .setSaveConsumer(x->BilibiliMedia.config.useBBDown = x)
                    .setDefaultValue(true)
                    .build());
            general.addEntry(entryBuilder.startBooleanToggle(Text.translatable("config.bilibili_media.enable_range_requests"), BilibiliMedia.config.enableRangeRequests)
                    .setSaveConsumer(x->BilibiliMedia.config.enableRangeRequests = x)
                    .setTooltip(Text.translatable("config.bilibili_media.enable_range_requests.tip"))
                    .setDefaultValue(false)
                    .build());
            general.addEntry(entryBuilder.startBooleanToggle(Text.translatable("config.bilibili_media.clear_on_start"), BilibiliMedia.config.clearOnStart)
                    .setSaveConsumer(x->BilibiliMedia.config.clearOnStart = x)
                    .setDefaultValue(false)
                    .requireRestart()
                    .build());
            builder.setSavingRunnable(() -> {
                AutoConfig.getConfigHolder(BiliBiliMediaConfig.class).setConfig(BilibiliMedia.config);
                AutoConfig.getConfigHolder(BiliBiliMediaConfig.class).save();
                BilibiliMedia.config = AutoConfig.getConfigHolder(BiliBiliMediaConfig.class).getConfig();
                SimpleFileServer.enableRangeRequests = BilibiliMedia.config.enableRangeRequests;
            });
            return builder.build();
        };
    }
}
